package vn.appsmobi.ui;

import vn.appsmobi.R;
import vn.appsmobi.model.AppInfo;
import vn.appsmobi.model.AppInfo.AppStatus;
import vn.appsmobi.ulti.DownloadInstallManager;
import vn.appsmobi.ulti.DownloadInstallManager.Progress;
import vn.appsmobi.ulti.Log;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;

public class ActionButton extends ActionArea {
	private TextView mPrimary = new TextView(this.mContext);
	
	public ActionButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mPrimary.setTextAppearance(getContext(),R.style.TextAppearance_Button_Action);		
	    this.mPrimary.setDuplicateParentStateEnabled(true);
	    this.mPrimary.setGravity(Gravity.CENTER_VERTICAL);
	    addView(this.mPrimary);
	    
	}

	@Override
	protected void bindInstalled(AppInfo appInfo) {		
		Log.v("bindInstalledbindInstalledbindInstalled:" + appInfo.getName());
		setEnabled(true);
		this.mPrimary.setBackgroundResource(0);
	    this.mPrimary.setText(getContext().getString(R.string.title_installed));
	}

	@Override
	protected void bindInstalling(AppInfo appInfo) {
		if(DownloadInstallManager.getManager().mInstallManuallyMap.containsKey(appInfo.getPackageName()))
		{
			setEnabled(true);
	
		}else
		{
			setEnabled(false);
			this.mPrimary.setBackgroundResource(0);
			this.mPrimary.setText(getContext().getString(R.string.title_installing));
		}
		
	    //this.mPrimary.setTextAppearance(getContext(), R.style.Widget_Button_Action);

	}

	@Override
	protected void bindNormal(AppInfo appInfo) {
		setEnabled(true);
		this.iClicked = false;
		this.mPrimary.setBackgroundResource(R.drawable.btn_action_bg);
	    this.mPrimary.setText(getContext().getString(R.string.title_install));
	    setOnClickListener(this.mArrangeClickListener);
	    //this.mPrimary.setTextAppearance(getContext(),  R.style.Widget_Button_Action);

	}

	@Override
	protected void bindPrice(AppInfo appInfo) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void bindUpdate(AppInfo appInfo) {
		setEnabled(true);
		this.iClicked = false;
		this.mPrimary.setBackgroundResource(R.drawable.btn_action_bg);
	    this.mPrimary.setText(getContext().getString(R.string.title_update));	
	    setOnClickListener(this.mArrangeClickListener);

	}

	@Override
	protected void updateProgressConnecting(AppInfo appInfo, Progress progress) {
		this.mPrimary.setBackgroundResource(0);
		this.mPrimary.setText(getContext().getString(R.string.title_connecting));
	}

	@Override
	protected void updateProgressDownloading(AppInfo appInfo, Progress progress) {
			setEnabled(false);
			this.mPrimary.setBackgroundResource(0);
			String str = "0";
			if ((progress.totalBytes > 0) && (progress.currBytes > 0))
		     str = String.valueOf(Math.round(100.0D * progress.currBytes / progress.totalBytes));		    
		    this.mPrimary.setText(getContext().getString(R.string.title_downloadx, str));

	}

	@Override
	protected void updateProgressInstalling(AppInfo appInfo, Progress progress) {
		if(DownloadInstallManager.getManager().mInstallManuallyMap.containsKey(appInfo.getPackageName()))
		{
			setEnabled(true);
			this.mPrimary.setText(getContext().getString(R.string.text_setup_install));
		}
		else
		{
			setEnabled(false);
			this.mPrimary.setBackgroundResource(0);
			this.mPrimary.setText(getContext().getString(R.string.title_installing));
		}
				
		
	}

	@Override
	protected void updateProgressPaused(AppInfo appInfo, Progress progress) {
		this.mPrimary.setText(getContext().getString(R.string.title_pause));

	}

	@Override
	protected void updateProgressPending(AppInfo appInfo, Progress progress) {
		this.mPrimary.setText(getContext().getString(R.string.title_pending));

	}

	@Override
	protected void updateProgressVerifying(AppInfo appInfo, Progress progress) {
		this.mPrimary.setBackgroundResource(0);
		this.mPrimary.setText(getContext().getString(R.string.title_verifying)); 

	}

	@Override
	protected void updateProgressInstalled(AppInfo appInfo, Progress progress) {
		this.mPrimary.setBackgroundResource(0);
		AppInfo  appInfo1 = AppInfo.get(appInfo.getId());
  	  	appInfo1.status = AppStatus.STATUS_INSTALLED;
		this.bindInstalled(appInfo1);
		
	}

}
